﻿using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace CodeCounsel.SharePoint.NavigationService
{
    [Guid("50c2366b-24a5-4b9a-ad47-e6d6177c630c")]
    public class NavigationService
        : SPService, IServiceAdministration
    {
        public NavigationService()
        {
        }

        public NavigationService(SPFarm farm)
            : base(String.Empty, farm)
        {
        }

        public SPServiceApplication CreateApplication(string name, 
            Type serviceApplicationType, 
            SPServiceProvisioningContext provisioningContext)
        {
            ValidateApplicationType(serviceApplicationType);
            return NavigationApplication.Create(name, this);
        }

        public SPServiceApplicationProxy CreateProxy(string name, 
            SPServiceApplication serviceApplication, 
            SPServiceProvisioningContext provisioningContext)
        {
            NavigationApplication application = serviceApplication as NavigationApplication;
            if (application == null)
            {
                throw new ArgumentException("Application type not supported");
            }
            NavigationServiceProxy serviceProxy = (NavigationServiceProxy)Farm.GetObject(
                String.Empty, Farm.Id, typeof(NavigationServiceProxy));
            if (null == serviceProxy)
            {
                serviceProxy = new NavigationServiceProxy(Farm);
                serviceProxy.Update(true);
            }
            NavigationApplicationProxy applicationProxy = serviceProxy.ApplicationProxies.GetValue<NavigationApplicationProxy>(name);
            if (applicationProxy == null)
            {
                applicationProxy = NavigationApplicationProxy.Create(
                    name, application, serviceProxy, false);
                applicationProxy.Update();
            }
            return applicationProxy;
        }

        public SPPersistedTypeDescription GetApplicationTypeDescription(Type serviceApplicationType)
        {
            ValidateApplicationType(serviceApplicationType);
            return new SPPersistedTypeDescription("Navigation Application (Demo)",
                "A service application that facilitates cross site navigation");
        }

        public override SPAdministrationLink GetCreateApplicationLink(Type serviceApplicationType)
        {
            ValidateApplicationType(serviceApplicationType);
            return new SPAdministrationLink("/_admin/CodeCounsel.SharePoint.NavigationService/NewApplication.aspx");
        }

        public override SPCreateApplicationOptions GetCreateApplicationOptions(Type serviceApplicationType)
        {
            ValidateApplicationType(serviceApplicationType);
            return SPCreateApplicationOptions.None;
        }

        public Type[] GetApplicationTypes()
        {
            return new Type[] { typeof(NavigationApplication) };
        }

        void ValidateApplicationType(Type serviceApplicationType)
        {
            if (serviceApplicationType != typeof(NavigationApplication))
            {
                throw new ArgumentException("Application type not supported");
            }
        }
    }
}
